package com.zhiyou100.video.webaction.User;

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhiyou100.video.model.User;
import com.zhiyou100.video.service.UserService;
import com.zhiyou100.video.utils.MD5Utils;
@SuppressWarnings("all")
@Controller("UserAction")
@Scope(scopeName="prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	private User uer = new User();
	@Override
	public User getModel() {
		return uer;
	}
	@Autowired
	UserService us;
	private File image;
	private String imageFileName;
	private String imageContentType;
	
	
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public String getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	/*修改信息后获取session*/
	public String userSession() throws Exception {
		User li = us.selectUserSession(uer);
		ActionContext.getContext().getSession().put("_front_user", li);
		return "personalcenter";
	}
	/*跳转到主页面*/
	public String userlogin() throws Exception {
		return SUCCESS;
	}
	/*个人中心主页*/
	public String personalcenter() throws Exception {
		return "personalcenter";
	}
	/*更改个人资料*/
	public String profile() throws Exception {
		return "profile";
	}
	/*保存修改的个人资料*/
	public String userprofile() throws Exception {
		us.updateUserProfile(uer);
		User li = us.selectUserSession(uer);
		ActionContext.getContext().getSession().put("_front_user", li);
		return "personalcenter";
	}
	/*修改头像*/
	public String avatar() throws Exception {
		
		return "avatar";
	}
	/*保存上传头像*/
	public String useravatar() throws Exception {
		
		String str = UUID.randomUUID().toString().replaceAll("-","");
		String ext = FilenameUtils.getExtension(imageFileName);
		String mage = "/ppt/"+str+"."+ext;
		uer.setHeadUrl(mage);
		FileUtils.copyFile(new File(image.getAbsolutePath()), new File("E:\\unload\\"+str+"."+ext));
		 us.updateUserAvatar(uer);
		 User li = us.selectUserSession(uer);
		ActionContext.getContext().getSession().put("_front_user", li);
		return "personalcenter";
	}
	/*修改密码*/
	public String password() throws Exception {
			return "password";
		}
	/*退出登录*/
	public String logout() throws Exception {
		ActionContext.getContext().getSession().remove("_front_user");
		return "userlogout";
	}
	/*忘记密码*/
	public String forgetpwd() throws Exception {
		
		return "forgetpwd";
	}
	
//	public String forget() throws Exception {
//		ActionContext.getContext().put("useid", uer.getId());
//		return "forget";
//	}
	/*重置密码*/
	public String resetpwd() throws Exception {
		uer.setPassword(MD5Utils.getMD5(uer.getPassword()));
		us.updateResetPwd(uer);
		return "userlogout";
	}
	
}
